package online.xuping.thousandsOfWorlds.sd;

import cn.hutool.core.lang.Dict;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.setting.yaml.YamlUtil;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class PromptDbDomain {
    private ArrayList<PromptDbRootDomain> root;

    public static void main(String[] args) {
        PromptDbDomain promptDbDomain = loadFormFile("E:\\workspace\\ThousandsOfWorlds\\src\\main\\resources\\config\\SdWords_zh_CN.yaml");


//      PromptDbDomain.showData(promptDbDomain);
        List<String> re  =promptDbDomain.findKey("服饰-正装");
        List<String> re2  =promptDbDomain.findValue("服饰-正装");

        System.out.println(re.stream().collect(Collectors.joining(",")));
        System.out.println(re2.stream().collect(Collectors.joining(",")));
    }

    /**
     * 通过路径获取 对应路径下所有的关键字（英文）
     * @param keyPath
     * @return
     */
    public List<String> findKey(String keyPath) {
        List<String> re = new ArrayList<>();
        String[] keys = keyPath.split("-");
        if (keys.length != 2) {
            return null;
        }
        root.stream().filter(it->it.getName().equals(keys[0])).forEach(it12->{
            //这里检查 后续的内容
            it12.getGroups().stream().filter(itt->itt.getName().equals(keys[1])).forEach(itt->{
                itt.getTags().forEach((k,v)->{
                    re.add(k);
                });
            });

        });
        return re;
    }
    public List<String> findValue(String keyPath) {
        List<String> re = new ArrayList<>();
        String[] keys = keyPath.split("-");
        if (keys.length != 2) {
            return null;
        }
        root.stream().filter(it->it.getName().equals(keys[0])).forEach(it12->{
            //这里检查 后续的内容
            it12.getGroups().stream().filter(itt->itt.getName().equals(keys[1])).forEach(itt->{
                itt.getTags().forEach((k,v)->{
                    re.add(v);
                });
            });

        });
        return re;
    }
    public static PromptDbDomain loadFormFile(String filepath) {
        Dict dict = YamlUtil.loadByPath(filepath);

        JSONObject root = JSONUtil.parseObj(dict);
        return  root.toBean(PromptDbDomain.class);

    }

    public static void showData( PromptDbDomain promptDbDomain){

        promptDbDomain.getRoot().stream().forEach(it->{

            it.getGroups().stream().forEach(it1->{
                System.out.print(it.getName());
                System.out.print("-");
                System.out.print (it1.getName());
                    System.out.print(":");
                    if (it1.getTags() != null) {
                        it1.getTags().forEach((k,v)->{
                            System.out.print(v);
                            System.out.print(",");
                        });
                    }
                System.out.println();
            });

        });
    }

}
